package deepbitview

/**
 * a very easy way to access the mtgox properties, required to login and out
 */
class MtGoxAccessHandler {

    static boolean isConfigured() {
        boolean valid = Mtgox.list().size() == 1

        if( Mtgox.list().size() > 1){
            Mtgox.list().each {Mtgox mtgox -> mtgox.delete(flush:true)}
        }

        return valid
    }

    static String getUserName() {
        return Mtgox.list().get(0).getUsername()
    }

    static String getPassword() {
        return Mtgox.list().get(0).getPassword()
    }

    /**
     * registers or replaces an account
     * @param userName
     * @param password
     */
    static void register(String userName, String password) {
        if (isConfigured()) {
            Mtgox.list().get(0).delete(flush: true)
        }

        Mtgox mt = new Mtgox()
        mt.username = userName
        mt.password = password
        mt.save(flush: true)

    }

}
